package com.zqxq.lottery.controller.esports;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.entity.o2o.esports.ESportsO2O;
import com.zqxq.lottery.entity.po.esports.ESportsPO;
import com.zqxq.lottery.entity.vo.esports.ESportsPlaywayVO;
import com.zqxq.lottery.entity.vo.esports.ESportsTitleVO;
import com.zqxq.lottery.entity.vo.esports.ESportsVO;
import com.zqxq.lottery.mapper.esports.ESportsPlaywayMapper;
import com.zqxq.lottery.service.esports.IESportsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class ESportsController extends ESportsBaseController {

    @Autowired
    private IESportsService eSportsService;
    @Autowired
    private ESportsPlaywayMapper eSportsPlaywayMapper;
    @PostMapping("/get")
    public Result<ESportsVO> getESportsById(@RequestParam String matchId){
        Result rs = new Result();
        try{
            ESportsVO vo = eSportsService.getESportsById(matchId);
            rs.setData(vo);
        }catch(Exception e){
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("getESportsById  Error:",e);
        }
        return rs;
    }

    @PostMapping("/homeList")
    public Result getESportsHomeList(Integer pageNO,Integer pageSize,Integer type){
        Result rs = new Result();
        Page<ESportsVO> page = new Page<>(pageNO,pageSize);
        List<ESportsVO> list = eSportsService.getESportsHomeList(page,type);
        page.setRecords(list);
        rs.setData(page);
        return rs;
    }

    @PostMapping("/titleList")
    public Result getESportsTitleList(Integer type){
        Result rs = new Result();
        List<ESportsTitleVO> list = eSportsService.getESportsTitleList(type);
        rs.setData(list);
        return rs;
    }

    @PostMapping("/playwayList")
    public Result getPlaywayListByMatchId(String matchId,String round){
        Result rs = new Result();
        if (StrUtils.isEmpty(matchId)) {
            rs.setCode(CodeEnum.FAIL_100002.getCode());
            rs.setMsg(CodeEnum.FAIL_100002.getMsg());
            return rs;
        }
        List<ESportsPlaywayVO> dataList = eSportsService.getPlaywayListByMatchId(matchId,round);
        rs.setData(dataList);
        return rs;

    }

    @PostMapping("/eSportsHead")
    public Result getESportsHead(String matchId){
        Result rs = new Result();
        if (StrUtils.isEmpty(matchId)) {
            rs.setCode(CodeEnum.FAIL_100002.getCode());
            rs.setMsg(CodeEnum.FAIL_100002.getMsg());
            return rs;
        }
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("match_id",matchId);
        ESportsPO eSportsPO = eSportsService.selectOne(wrapper);
        ESportsVO vo = ESportsO2O.toVO(eSportsPO);
        String roundStrs = eSportsPlaywayMapper.getRounds(matchId);
        vo.setRounds(roundStrs);
        rs.setData(vo);
        return rs;
    }

}
